package ar.com.ford.it.fullEconomics.view.form;

import java.util.Calendar;
import java.util.Collection;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;
import org.apache.struts.validator.ValidatorForm;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesr03Agreement;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesr04Agreemitem;
import ar.com.ford.it.fullEconomics.util.FullEconomicsDateUtil;

public class AgreementDetailForm extends ValidatorForm {

    private String section;
    private String editBlocked;
    private String selectedIndex;
    private String commodity;
    private String partClass;
    private String prefix;
    private String base;
    private String suffix;
    private String percentIncrease;
    private String newPrice;
    private String currencyCode;
    private String effectiveDate;
    private String fulleeCode;
    private String vehicleLine;
    private String agreementScope;
    private String agreementClass;
    private String comments;
    private Teesr03Agreement agreement;
    private Teesr04Agreemitem agreemItemSelected;
    private Collection<Teesr04Agreemitem> items;

    public void resetForm() {
        this.section = null;
        this.editBlocked = "false";
        this.agreementClass = null;
        this.agreementScope = null;
        this.agreemItemSelected = null;
        this.base = null;
        this.prefix = null;
        this.suffix = null;
        this.commodity = "-1";
        this.currencyCode = "-1";
        this.effectiveDate = null;
        this.vehicleLine = "-1";
        this.fulleeCode = null;
        this.items = null;
        this.agreement = null;
    }
    
    public void cleanCreateFields(){
        this.editBlocked = "false";
        this.agreementClass = null;
        this.agreementScope = null;
        this.partClass = "-1";
        this.base = null;
        this.prefix = null;
        this.suffix = null;
        this.commodity = "-1";
        this.currencyCode = "-1";
        this.effectiveDate = null;
        this.vehicleLine = "-1";
        this.fulleeCode = null;
    }

    public ActionErrors validate(ActionMapping mapping,
                                 HttpServletRequest request) {

        ActionErrors ae = new ActionErrors();
        MessageResources resources = (MessageResources) request
                .getAttribute(org.apache.struts.Globals.MESSAGES_KEY);
        if (this.getSection().equalsIgnoreCase("createUpdate")) {
            if (StringUtils.isBlank(this.getAgreementScope())) {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "agreementDetail.error.invalidAgreementScope"));
            } else if (this.getAgreementScope().equalsIgnoreCase("commodity")) {
                if (this.getCommodity().equalsIgnoreCase("-1")) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "agreementDetail.error.invalidCommodity"));
                }
            } else if (this.getAgreementScope().equalsIgnoreCase("part")) {
                if (this.getPartClass().equalsIgnoreCase("-1")) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "agreementDetail.error.invalidPart"));
                }else if (StringUtils.isBlank(this.getBase())) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "agreementDetail.error.invalidPart"));
                }
            }
            if (StringUtils.isBlank(this.getAgreementClass())) {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "agreementDetail.error.invalidAgreementClass"));
            } else if (this.getAgreementClass().equalsIgnoreCase("I")) {
                try {

                    if (Double.valueOf(this.getPercentIncrease()) <= 0)
                        ae
                                .add(
                                        ActionMessages.GLOBAL_MESSAGE,
                                        new ActionMessage(
                                                "agreementDetail.error.invalidPercentIncrease"));
                } catch (Exception e) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "agreementDetail.error.invalidPercentIncrease"));
                }
            } else if (this.getAgreementClass().equalsIgnoreCase("P")) {
                try {
                    if (Double.valueOf(this.getNewPrice()) <= 0)
                        ae
                                .add(
                                        ActionMessages.GLOBAL_MESSAGE,
                                        new ActionMessage(
                                                "agreementDetail.error.invalidNewPrice"));
                } catch (Exception e) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "agreementDetail.error.invalidNewPrice"));
                }
                if (this.getCurrencyCode().equalsIgnoreCase("-1")) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "agreementDetail.error.invalidCurrency"));
                }
            }
            if (StringUtils.isBlank(this.getEffectiveDate())) {
                ae
                        .add(
                                ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage(
                                        "errors.required",
                                        resources
                                                .getMessage("agreementDetail.label.effectiveDate")));
            } else {
                try {
                    if (FullEconomicsDateUtil.format(this.getEffectiveDate()) == null) {
                        ae
                                .add(
                                        ActionMessages.GLOBAL_MESSAGE,
                                        new ActionMessage(
                                                "errors.date",
                                                resources
                                                        .getMessage("agreementDetail.label.effectiveDate")));
                    }
                } catch (Exception e) {
                    ae
                            .add(
                                    ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "errors.date",
                                            resources
                                                    .getMessage("agreementDetail.label.effectiveDate")));
                }
            }
        }
        return ae;
    }

    public Teesr04Agreemitem getAgreemItemByIndex(String index) {
        List items = (List) this.getItems();
        return (Teesr04Agreemitem) items.get(Integer.parseInt(index) - 1);
    }

    public void setItemForEdit(Teesr04Agreemitem item) {
        if (item.getTeesp04Supply() != null) {
            this.setPartClass(item.getTeesp04Supply().getTeesp01Part()
                    .getEesp01PartClassCodeC());
            this.setPrefix(item.getTeesp04Supply().getTeesp01Part()
                    .getEesp01PartPrefixCodeC());
            this.setBase(item.getTeesp04Supply().getTeesp01Part()
                    .getEesp01PartBaseCodeC());
            this.setSuffix(item.getTeesp04Supply().getTeesp01Part()
                    .getEesp01PartSuffixCodeC());
            this.setAgreementScope("part");
        } else if (item.getTeesp02Commodity() != null) {
            this.setCommodity(item.getTeesp02Commodity()
                    .getEesp02CommodityNumberK().toString());
            this.setAgreementScope("commodity");
        } else {
            this.setAgreementScope("allParts");
        }
        if (item.getEesr04AgritmIncrPercentP() != null) {
            this.setPercentIncrease(item.getEesr04AgritmIncrPercentP()
                    .toString());
            this.setAgreementClass("I");
        } else {
            this.setNewPrice(item.getEesr04AgritmNewpriceAmounA().toString());
            this.setCurrencyCode(item.getEesr04CurrencyCodeC()
                    .getEesm01CurrencyCodeC());
            this.setAgreementClass("P");
        }
        if (item.getNcas012VehicleVw() == null) {
            this.setVehicleLine("-1");
        } else {
            this.setVehicleLine(item.getNcas012VehicleVw()
                    .getCas012VehicleCodeC());
        }
        Calendar calEffDate = Calendar.getInstance();
        calEffDate.setTimeInMillis(item.getEesr04AgritmEffectiveDateY()
                .getTime());
        this.setEffectiveDate(FullEconomicsDateUtil.format(calEffDate));
        if (item.getTeesf01Fulleconom() != null) {
            this.setFulleeCode(item.getTeesf01Fulleconom()
                    .getEesf01FulleNumberK().toString());
        }
    }

    public Teesr04Agreemitem getAgreemItemSelected() {
        return agreemItemSelected;
    }

    public void setAgreemItemSelected(Teesr04Agreemitem agreemItemSelected) {
        this.agreemItemSelected = agreemItemSelected;
    }

    public String getCurrencyCode() {
        return currencyCode;
    }

    public void setCurrencyCode(String currencyCode) {
        this.currencyCode = currencyCode;
    }

    public String getEffectiveDate() {
        return effectiveDate;
    }

    public void setEffectiveDate(String effectiveDate) {
        this.effectiveDate = effectiveDate;
    }

    public Collection<Teesr04Agreemitem> getItems() {
        return items;
    }

    public void setItems(Collection<Teesr04Agreemitem> items) {
        this.items = items;
    }

    public String getNewPrice() {
        return newPrice;
    }

    public void setNewPrice(String newPrice) {
        this.newPrice = newPrice;
    }

    public String getPercentIncrease() {
        return percentIncrease;
    }

    public void setPercentIncrease(String percentIncrease) {
        this.percentIncrease = percentIncrease;
    }

    public String getSection() {
        return section;
    }

    public void setSection(String section) {
        this.section = section;
    }

    public String getBase() {
        return base;
    }

    public void setBase(String base) {
        this.base = base;
    }

    public String getCommodity() {
        return commodity;
    }

    public void setCommodity(String commodity) {
        this.commodity = commodity;
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }

    public String getAgreementScope() {
        return agreementScope;
    }

    public void setAgreementScope(String agreementScope) {
        this.agreementScope = agreementScope;
    }

    public String getAgreementClass() {
        return agreementClass;
    }

    public void setAgreementClass(String agreementClass) {
        this.agreementClass = agreementClass;
    }

    public String getPartClass() {
        return partClass;
    }

    public void setPartClass(String partClass) {
        this.partClass = partClass;
    }

    public String getFulleeCode() {
        return fulleeCode;
    }

    public void setFulleeCode(String fulleeCode) {
        this.fulleeCode = fulleeCode;
    }

    public String getVehicleLine() {
        return vehicleLine;
    }

    public void setVehicleLine(String vehicleLine) {
        this.vehicleLine = vehicleLine;
    }

    public Teesr03Agreement getAgreement() {
        return agreement;
    }

    public void setAgreement(Teesr03Agreement agreement) {
        this.agreement = agreement;
    }

    public String getEditBlocked() {
        return editBlocked;
    }

    public void setEditBlocked(String editBlocked) {
        this.editBlocked = editBlocked;
    }

    public String getSelectedIndex() {
        return selectedIndex;
    }

    public void setSelectedIndex(String selectedIndex) {
        this.selectedIndex = selectedIndex;
    }

    public String getComments() {
        return comments;
    }

    public void setComments(String comments) {
        this.comments = comments;
    }
}
